package com.vehiclemanagement.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehiclemanagement.entity.TrafficViolation;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 交通违章记录Mapper接口
 */
public interface TrafficViolationMapper extends BaseMapper<TrafficViolation> {

    /**
     * 自定义分页查询，包含关联的车辆和驾驶员信息
     *
     * @param page     分页参数
     * @param whereSql 自定义WHERE条件
     * @return 包含车辆和驾驶员信息的分页结果
     */
    @Select("SELECT tv.*, v.plate_number, d.name AS driver_name " +
            "FROM traffic_violation tv " +
            "LEFT JOIN vehicle v ON tv.vehicle_id = v.id " +
            "LEFT JOIN driver d ON tv.driver_id = d.id " +
            "${whereSql} " +
            "ORDER BY tv.violation_time DESC")
    IPage<TrafficViolation> selectPageWithInfo(Page<TrafficViolation> page, @Param("whereSql") String whereSql);

    /**
     * 计算指定车辆的累计罚款金额
     *
     * @param vehicleId 车辆ID
     * @return 累计罚款金额
     */
    @Select("SELECT SUM(fine_amount) FROM traffic_violation WHERE vehicle_id = #{vehicleId} AND del_flag = 0")
    BigDecimal sumFineAmountByVehicleId(@Param("vehicleId") Long vehicleId);

    /**
     * 计算指定驾驶员的累计罚款金额
     *
     * @param driverId 驾驶员ID
     * @return 累计罚款金额
     */
    @Select("SELECT SUM(fine_amount) FROM traffic_violation WHERE driver_id = #{driverId} AND del_flag = 0")
    BigDecimal sumFineAmountByDriverId(@Param("driverId") Long driverId);

    /**
     * 计算指定车辆的累计扣分
     *
     * @param vehicleId 车辆ID
     * @return 累计扣分
     */
    @Select("SELECT SUM(point_deduction) FROM traffic_violation WHERE vehicle_id = #{vehicleId} AND del_flag = 0")
    Integer sumPointDeductionByVehicleId(@Param("vehicleId") Long vehicleId);

    /**
     * 计算指定驾驶员的累计扣分
     *
     * @param driverId 驾驶员ID
     * @return 累计扣分
     */
    @Select("SELECT SUM(point_deduction) FROM traffic_violation WHERE driver_id = #{driverId} AND del_flag = 0")
    Integer sumPointDeductionByDriverId(@Param("driverId") Long driverId);

    /**
     * 按月统计违章数量和罚款金额
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 月度统计结果
     */
    @Select("SELECT DATE_FORMAT(violation_time, '%Y-%m') AS month, " +
            "COUNT(*) AS violation_count, " +
            "SUM(fine_amount) AS total_fine_amount " +
            "FROM traffic_violation " +
            "WHERE violation_time BETWEEN #{startTime} AND #{endTime} " +
            "AND del_flag = 0 " +
            "GROUP BY DATE_FORMAT(violation_time, '%Y-%m') " +
            "ORDER BY month")
    List<Map<String, Object>> statisticsByMonth(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 按违章类型统计违章数量和罚款金额
     *
     * @return 违章类型统计结果
     */
    @Select("SELECT violation_type, " +
            "COUNT(*) AS violation_count, " +
            "SUM(fine_amount) AS total_fine_amount, " +
            "SUM(point_deduction) AS total_point_deduction " +
            "FROM traffic_violation " +
            "WHERE del_flag = 0 " +
            "GROUP BY violation_type " +
            "ORDER BY violation_count DESC")
    List<Map<String, Object>> statisticsByViolationType();

    /**
     * 物理删除记录
     *
     * @param id 记录ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM traffic_violation WHERE id = #{id}")
    int deletePhysically(@Param("id") Long id);
} 